cap program drop tab_sum_stats_demographics
program define tab_sum_stats_demographics
	
	shell mkdir -p "$release/tables/sum_stats/"
	use "$sumstats_data_path/harmonized_subs.dta", clear
	
	#delimit ;
	collapse 
		(mean) mean_single_mem_ind = single_mem
		(sd)   sd_single_mem_ind   = single_mem
		(p50)  p50_single_mem_ind  = single_mem
		(mean) mean_married_nokids_ind = married_nokids
		(sd)   sd_married_nokids_ind   = married_nokids
		(p50)  p50_married_nokids_ind  = married_nokids
		(mean) mean_unmarried_wkids_ind = unmarried_wkids
		(sd)   sd_unmarried_wkids_ind   = unmarried_wkids
		(p50)  p50_unmarried_wkids_ind  = unmarried_wkids
		(mean) mean_married_wkids_ind = married_wkids
		(sd)   sd_married_wkids_ind   = married_wkids
		(p50)  p50_married_wkids_ind  = married_wkids
		(mean) mean_ndeps_positive = ndeps_positive
		(sd)   sd_ndeps_positive   = ndeps_positive
		(p50)  p50_ndeps_positive  = ndeps_positive
		(mean) mean_sum_concurrent_risk = sum_concurrent_risk
		(sd)   sd_sum_concurrent_risk   = sum_concurrent_risk
		(p50)  p50_sum_concurrent_risk  = sum_concurrent_risk
		(mean) mean_age = age
		(sd)   sd_age   = age
		(p50)  p50_age  = age
		(mean) mean_income = best_guess_incomeoverfpl
		(sd)   sd_income   = best_guess_incomeoverfpl
		(p50)  p50_income  = best_guess_incomeoverfpl
		(mean) mean_over50_ind = over50
		(sd)   sd_over50_ind   = over50
		(p50)  p50_over50_ind  = over50
		(mean) mean_urban_ind = real_urban
		(sd)   sd_urban_ind   = real_urban
		(p50)  p50_urban_ind  = real_urban
		, by(sg_vs_indiv)
	;
	#delimit cr
	
	reshape long mean_ sd_ p50_, i(sg_vs_indiv) j(dem_var, string)
	reshape wide mean_ sd_ p50_, i(dem_var) j(sg_vs_indiv)
	
	local dem_vars = "single_mem_ind married_nokids_ind unmarried_wkids_ind married_wkids_ind ndeps_positive sum_concurrent_risk income age over50_ind urban_ind"
	word_orderer "`dem_vars'"
	drop_ind_sd "`dem_vars'" "0 1"
	
	export delimited using "$release/tables/sum_stats/demographics.csv", replace

end

cap program drop tab_sum_stats_insurer_spending
program define tab_sum_stats_insurer_spending
	
	use "$sumstats_data_path/harmonized_subs.dta", clear
	keep if est_sample
	
	gen zerospending_ind = (cost == 0)
	destring best_guess_subsidy , replace
	gen disp_metal = 0
	replace disp_metal = 1 if best_guess_metal == 2
        replace disp_metal = 2 if best_guess_metal == 3
	*replace disp_metal = 2 if best_guess_metal == 3 & ((all_subsidy==0 & sg_vs_indiv==0) | (sg_vs_indiv==1))
	*replace disp_metal = 3 if best_guess_metal == 3 & all_subsidy==1 & sg_vs_indiv==0
	replace disp_metal = 3 if best_guess_metal == 4
	
	
	foreach var in zerospending_ind cost_positive p {
		forvalues d = 1/3 {
			gen `var'_`d' = `var' if disp_metal == `d'
		}
	}
	
	local collapse_spell = ""
	foreach var in zerospending_ind cost_positive p {
		local newspell = "(mean) mean_`var'=`var' (sd) sd_`var'=`var' (p50) p50_`var'=`var'"
		local collapse_spell = "`collapse_spell' `newspell'"
		forvalues d = 1/3 {
			local newspell = "(mean) mean_`var'_`d'=`var'_`d' (sd) sd_`var'_`d'=`var'_`d' (p50) p50_`var'_`d'=`var'_`d'"
			local collapse_spell = "`collapse_spell' `newspell'"
		}
	}
	forvalues d = 1/3 {
		gen disp_metal_`d' = (disp_metal==`d')
		local collapse_spell = "`collapse_spell' (mean) mean_mktshr_`d'=disp_metal_`d' "
	}
	
	#delimit ;
	collapse 
		`collapse_spell'
		, by(sg_vs_indiv)
	;
	#delimit cr
	
		
	reshape long mean_ sd_ p50_, i(sg_vs_indiv) j(dem_var, string)
	reshape wide mean_ sd_ p50_, i(dem_var) j(sg_vs_indiv)
	
	local orderer = ""
	foreach var in "zerospending_ind" "cost_positive" "p" "mktshr"{
		local orderer = "`orderer' `var'"
		forvalues d = 1/3 {
			local orderer = "`orderer' `var'_`d'"
		}
	}
	di "`orderer'"
	word_orderer "`orderer'"
	drop_ind_sd "`orderer'" "0 1"
	
	export delimited using "$release/tables/sum_stats/insurer_spending.csv", replace
	
end


cap program drop tab_sum_stats_count
program define tab_sum_stats_count
	use "$sumstats_data_path/harmonized_subs.dta", clear

	egen tag_subscriber = tag(subscriberid)
	
	collapse (sum) tag_subscriber (count) tag_subscriber_year=subscriberid, by(sg_vs_indiv)
	reshape long tag_, i(sg_vs_indiv) j(dem_var, string)
	reshape wide tag_, i(dem_var) j(sg_vs_indiv)	
	
	word_orderer "insurer_ra1 insurer_ra2 insurer_ra3 insurer_rural subscriber subscriber_year"
	export delimited using "$release/tables/sum_stats/count.csv", replace
end


cap program drop tab_sum_stats_count_insurance
program define tab_sum_stats_count_insurance
	use "$sumstats_data_path/harmonized_subs.dta", clear
	keep if est_sample

	egen tag_insurer = tag(payer_id)
	egen tag_insurer_ra1 = tag(payer_id sg_vs_indiv) if inlist(best_guess_ra, 1, 2, 3)
	egen tag_insurer_rural = tag(payer_id sg_vs_indiv best_guess_ra) if !inlist(best_guess_ra, 1, 2, 3)
	replace tag_insurer_rural = tag_insurer_rural / 4 // average for rural areas, since there are 4 of them
	egen tag_subscriber = tag(subscriberid)
	
	collapse (sum) tag_insurer_* (sum) tag_subscriber (count) tag_subscriber_year=subscriberid, by(sg_vs_indiv)
	reshape long tag_, i(sg_vs_indiv) j(dem_var, string)
	reshape wide tag_, i(dem_var) j(sg_vs_indiv)	
	
	word_orderer "insurer_ra1 insurer_ra2 insurer_ra3 insurer_rural subscriber subscriber_year"
	export delimited using "$release/tables/sum_stats/count_insurance.csv", replace
end


cap program drop tab_sum_stats_sg_size
program define tab_sum_stats_sg_size
	use "$sumstats_data_path/harmonized_subs.dta", clear
	gen n=1
	keep if sg_vs_indiv
	collapse (count) n, by(contractnum year) 

	#delimit ;
	collapse 
		(mean) mean = n
		(p50)  p50	= n
		(sd)   sd   = n
		(min)  min  = n
		(max)  max  = n
		, by(year)
	;
	#delimit cr
	export delimited using "$release/tables/sum_stats/group_size.csv", replace
	

end


